# from django.core.handlers.wsgi import WSGIRequest
# WSGIRequest 继承于 HttpRequest
# from django.http import HttpRequest
from django.http import JsonResponse
from django.http.response import HttpResponse
from django.views.decorators.http import require_http_methods

# Create your views here.
@require_http_methods(["POST", "GET"])
def index(request):
    # response = "<h1>hello</h1>"
    # return HttpResponse(response, content_type="text/html")
    return HttpResponse("ok, goods.index")

@require_http_methods(["GET"])
def goods(request):
    # response = "<h1>hello</h1>"
    # return HttpResponse(response, content_type="text/html")

    """
        http://127.0.0.1:8000/goods/goods/?name=clay&pwd=123
        request.GET 获取URL后面的参数 打印 <QueryDict: {'name': ['clay'], 'pwd': ['123']}>
        QueryDict不是原生字典，不过支持字典的功能
        注意这里无论请求是GET、POST等任何方法，都可以用request.GET获取到数据
    """
    # print(request.GET)
    # print(request.GET.get("name"))
    # print(request.GET.get("pwd"))


    """
        http://127.0.0.1:8000/goods/goods/?name=clay&lve=game&lve=sing
        原生Dict获取lve时只能获取game，QueryDict有个 getlist 接口，可以获取多个
        这里打印 ['game', 'sing']
    """
    # print(request.GET.getlist("lve"))

    return HttpResponse("ok, goods.goods")


def goods2(request):
    """
        http://127.0.0.1:8000/goods/goods2/
        这里 request.POST 只能获取POST方法的请求体
        打印：
        <QueryDict: {'name': ['clay'], 'pwd': ['123']}>
        clay
        123
    """
    # print(request.POST)
    # print(request.POST.get("name"))
    # print(request.POST.get("pwd"))

    """
        http://127.0.0.1:8000/goods/goods2/
        同样的，也有getlist，打印 ['game', 'sing']
    """
    # print(request.body)
    # print(request.POST.getlist("lve"))

    """
        请求体传JSON
        {
            "name": "clay",
            "age": "22"
        }
        打印结果:
        b'{\r\n    "name": "clay",\r\n    "age": "22"\r\n}'
        {'name': 'clay', 'age': '22'}
    """
    print(request.body)
    import json
    print(json.loads(request.body))

    return HttpResponse("ok, goods.goods2")

def goods3(request):
    # print(request.META) # 原生请求头
    # print(request.method) # 请求方法

    """
    headers处理过的请求头，字典
    {
        'Content-Length': '42',
        'Content-Type': 'application/json',
        'User-Agent': 'PostmanRuntime/7.37.3',
        'Accept': '*/*',
        'Postman-Token':
        '47d3d76d-39cb-44c2-9a6b-897c9e4a1fdf',
        'Host': '127.0.0.1:8000',
        'Accept-Encoding':
        'gzip, deflate, br',
        'Connection':
        'keep-alive'
    }
    """
    print(request.headers)
    return HttpResponse("ok, goods.goods3")

"""返回json数据"""
def goods4(request):
    # data = {
    #     "id": 1,
    #     "name": "clay",
    #     "age": 22
    # }
    # import json
    # json_data = json.dumps(data)

    data = {
        "id": 1,
        "name": "clay",
        "age": 22
    }
    return JsonResponse(data)

"""返回图片"""
def goods5(request):
    file = open("./message_format.png", mode="rb").read()
    data = file.read()
    return HttpResponse(data, content_type="image/png")